package com.cgly.medical.security;

import com.alibaba.fastjson.JSON;
import com.cgly.medical.util.Msg;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: medical
 * @description: 用户认证失败处理器
 * @author: chen di
 * @create: 2022-08-15 16:32
 */
@Component
public class MyAuthenticationFailHandler implements AuthenticationFailureHandler {
    /**
     * 认证失败时调用
     * @param request
     * @param response
     * @param exception
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        System.out.println("登录失败");
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(Msg.fail().mess("用户名获密码错误")));
    }
}